#include "spi1.h"
#include "stm32f10x.h"

void SPI1_InitConfig(void)
{
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
	//1.配置GPIO
	GPIO_InitTypeDef SPI_GPIO;
	SPI_GPIO.GPIO_Pin = SPI1_SCK| SPI1_MISO | SPI1_MOSI;
	SPI_GPIO.GPIO_Mode = GPIO_Mode_AF_PP;//复用推挽输出
	SPI_GPIO.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA,&SPI_GPIO);
	
	SPI_GPIO.GPIO_Pin = SPI1_NSS;
	SPI_GPIO.GPIO_Mode = GPIO_Mode_Out_PP;//通用推挽输出
	SPI_GPIO.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA,&SPI_GPIO);
	
	GPIO_SetBits(GPIOA,SPI1_NSS);//取消选择状态
	
	//2.SPI配置
	SPI_I2S_DeInit(SPI1);
	SPI_InitTypeDef SPI1_SPI;
	SPI1_SPI.SPI_Direction = SPI_Direction_2Lines_FullDuplex;//双线双向全双工
	SPI1_SPI.SPI_Mode = SPI_Mode_Master;		//主机模式
	SPI1_SPI.SPI_DataSize = SPI_DataSize_8b;	//8位数据模式
	SPI1_SPI.SPI_CPOL = SPI_CPOL_High;		//时钟极性高
	SPI1_SPI.SPI_CPHA = SPI_CPHA_2Edge;		//第二个边沿
	SPI1_SPI.SPI_NSS = SPI_NSS_Soft;			//由软件控制
	SPI1_SPI.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4;
	SPI1_SPI.SPI_FirstBit = SPI_FirstBit_MSB;//高位先发
	SPI1_SPI.SPI_CRCPolynomial = 7;
	SPI_Init(SPI1,&SPI1_SPI);
	//3.配置更新中断
	//SPI_I2S_ITConfig(SPI1,SPI_I2S_IT_RXNE,ENABLE);
	//4.使能SPI1
	SPI_Cmd(SPI1,ENABLE);

}

